package com.stock_t.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stock_t.annotation.QueryCondition;

import java.lang.reflect.Field;

public class QueryWrapperUtils {
    
    /**
     * 为任意实体类创建QueryWrapper，只将非空字段作为查询条件
     * @param entity 查询条件对象
     * @param <T> 实体类类型
     * @return QueryWrapper<T>
     */
    public static <T> QueryWrapper<T> createQueryWrapper(T entity) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        
        if (entity == null) {
            return queryWrapper;
        }
        
        Class<?> clazz = entity.getClass();
        Field[] fields = clazz.getDeclaredFields();
        
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                Object value = field.get(entity);
                if (value != null && !"".equals(value)) {
                    // 获取字段上的注解
                    QueryCondition condition = field.getAnnotation(QueryCondition.class);
                    
                    // 获取列名
                    String columnName;
                    if (condition != null && !condition.columnName().isEmpty()) {
                        columnName = condition.columnName();
                    } else {
                        // 将驼峰命名转换为下划线命名
                        columnName = camelToUnderline(field.getName());
                    }
                    
                    // 根据注解指定的查询类型添加条件
                    if (condition != null) {
                        switch (condition.value()) {
                            case LIKE:
                                queryWrapper.like(columnName, value);
                                break;
                            case GT:
                                queryWrapper.gt(columnName, value);
                                break;
                            case LT:
                                queryWrapper.lt(columnName, value);
                                break;
                            case GE:
                                queryWrapper.ge(columnName, value);
                                break;
                            case LE:
                                queryWrapper.le(columnName, value);
                                break;
                            case EQ:
                            default:
                                queryWrapper.eq(columnName, value);
                                break;
                        }
                    } else {
                        // 默认行为:使用eq
                        queryWrapper.eq(columnName, value);
                    }
                }
            } catch (IllegalAccessException e) {
                // 忽略无法访问的字段
            }
        }
        
        return queryWrapper;
    }
    
    /**
     * 驼峰命名转下划线命名
     * @param camelCase 驼峰命名字符串
     * @return 下划线命名字符串
     */
    private static String camelToUnderline(String camelCase) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < camelCase.length(); i++) {
            char c = camelCase.charAt(i);
            if (Character.isUpperCase(c)) {
                if (i > 0) {
                    result.append("_");
                }
                result.append(Character.toLowerCase(c));
            } else {
                result.append(c);
            }
        }
        return result.toString();
    }
}